<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { loginApi } from '@/api/login'

const router = useRouter()
// 表单验证规则
const userRuleRef = ref()
const userRules = ref({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度应在6到16个字符之间', trigger: 'blur' },
  ]
})
// 表单数据
const form = ref({
  phone: '',
  password: '',
});
// 提交登录
const onSubmit = async () => {
  /* 判断表单引用是否存在 */
  if(!userRuleRef.value) return
  userRuleRef.value.validate(async (valid) => {
    /* 判断表单验证是否通过 */
    if(valid){
      let result = await loginApi(form.value)
      if(result.code){
        ElMessage.success('登录成功')
        if(result.data.userType === 1){
          localStorage.setItem('jobseekerInfo', JSON.stringify(result.data))
          router.push('/jobseeker')
        }else if(result.data.userType === 2){
          localStorage.setItem('hrInfo', JSON.stringify(result.data))
          router.push('/hr')
        }else if(result.data.userType === 3){
          localStorage.setItem('adminInfo', JSON.stringify(result.data))
          router.push('/admin')
        }
      }else{
        ElMessage.error(result.msg)
      }
    }else{
      ElMessage.error('表单验证不通过')  //表单验证失败提示信息
    }
  })
};
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">登录页面</h2>
      <!-- 登录表单 -->
      <el-form :model="form" :rules="userRules" ref="userRuleRef" label-width="80px" class="login-form">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" prefix-icon="User" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" prefix-icon="Lock" type="password" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="width: 100%" >
            登&nbsp;&nbsp;录
          </el-button>
        </el-form-item>
      </el-form>
      <p class="register-link">没有账号？<a href="/register">立即注册</a></p>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  /* 新背景设置 */
  background: url('background.jpg') center/cover no-repeat;
  /* 可选：添加半透明遮罩增强表单可读性 */
  position: relative;
}
/* 添加背景遮罩层（可选） */
.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(255, 255, 255, 0.7); /* 白色半透明遮罩 */
  z-index: 0;
}
.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #6a5acd;
  font-size: 34px;
  font-weight: 600;
  letter-spacing: 1px;
  position: relative;
  padding-bottom: 15px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); 
  font-family: 'kaiti';
}

.login-box {
  width: 400px;
  padding: 30px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  position: relative; /* 确保表单在遮罩上层 */
  z-index: 1;
}
.login-box {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.3); /* 改为半透明白色背景 */
  backdrop-filter: blur(3px); /* 可选：添加毛玻璃效果 */
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.login-form {
  margin-top: 20px;
}
.register-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #666;
}
.register-link a {
  color: #409EFF;
  text-decoration: none;
  margin-left: 5px;
}
.register-link a:hover {
  text-decoration: underline;
}
</style>